<template>
  <div>
    <nav-bar :title="title" :isShow="false"></nav-bar>
    <van-form @submit="onSubmit">
      <van-field
        v-model="name"
        name="name"
        label="公司名称"
        placeholder="公司名称"
        :rules="[{ required: true, message: '请填写公司名称' }]"
      />
      <!-- <van-field
        v-model="contact_person"
        name="contact_person"
        label="联系人"
        placeholder="联系人"
      />
      <van-field
        v-model="contact_tel"
        name="contact_tel"
        label="联系电话"
        placeholder="联系电话"
      /> -->
      <van-field
        v-model="agency"
        name="agency"
        label="授权状态"
        placeholder="授权状态"
        readonly
      />
      <div style="margin: 16px;">
        <van-button v-if="is_agency===0" round block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
    <div style="margin: 16px;" v-if="is_agency===1">
      <img :src="img_url" style="width:100%;"/>
      <h3 style="text-align:center;">代理授权二维码</h3>
    </div>

    <go-home-button v-if="is_agency===1"></go-home-button>

  </div>
</template>

<script>
import NavBar from '@/components/NavBar'
import GoHomeButton from '@/components/GoHomeButton'
import { agency_company_info, agency_company_add, agency_company_update, show_bg_qrcode, company_info_by_name } from '@/api/service'

export default {
  components: {
    NavBar,
    GoHomeButton,
  },
  props: {
    company_id: String
  },
  data() {
    return {
      title: this.$route.meta.title || "",
      id: 0,
      openid: "",
      office_openid: "",
      name: "",
      // contact_person: "",
      // contact_tel: "",
      is_agency: 0,
      img_url: "",
    };
  },
  computed:{
    agency(){
      if(this.is_agency === 1){
        return '已经授权'
      }else{
        return '未授权'
      }
    }
  },
  methods: {
    async onSubmit(values) {

      try {

        const { data } = await company_info_by_name(values)
        if(data.count > 0){
          this.$notify({ type: 'warning', message: '已经授权, 无法再次授权!' })
          return
        }
        const openid = localStorage.getItem("openid")
           
        values["office_openid"] = openid

        console.log("onSubmit:", values)

        if(this.id===0){
          this.addAgencyContractInfo(values)   //增加
        }else{
          values["id"] = this.id
          this.updateAgencyContractInfo(values)
        }
      } catch (error) {
        console.log(error.message);
        this.$notify({ type: 'warning', message: '读取数据失败' })
        return
      }

    },

    getQRCode(){
      if(!this.company_id) return
      const url = 'http://' + window.location.host + '/#/agency_person/' + this.company_id
      console.log(url)
      const params = {
          url: url,
          code: "rights",
        }
      show_bg_qrcode(params).then(res=>{
        return 'data:image/png;base64,' + btoa( new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), ''));
      }).then(data=>{
        this.img_url = data
      })
    },
    addAgencyContractInfo(contractInfo){
      agency_company_add(contractInfo).then(res=>{
        console.log("addAgencyContractInfo:", res)
        if(res.data.id){
          this.$router.push(`/agency_confirm/${res.data.id}`)
        }
      }).catch(error => {
        if (error.response) {
          console.log(error.response.data);
        } else if (error.request) {
          console.log(error.request);
        } else {
          console.log('Error', error.message);
        }
        console.log(error.config);
      })
    },

    updateAgencyContractInfo(contractInfo){
        agency_company_update(contractInfo).then(res=>{
          console.log('updateAgencyContractInfo', res)
          if(res.data.id){
            this.$router.push(`/agency_confirm/${res.data.id}`)
          }
        }).catch(error => {
          if (error.response) {
            console.log(error.response.data);
          } else if (error.request) {
            console.log(error.request);
          } else {
            console.log('Error', error.message);
          }
          console.log(error.config);
        })
    },

    getAgencyCompanyInfo(){
      if(!this.company_id)
          return 
      const openid = localStorage.getItem('openid')
      if(openid){
        agency_company_info(this.company_id).then(res => {
          console.log("getAgencyCompanyInfo:",res)
          this.id = res.data.id
          this.name = res.data.name
          this.contact_person = res.data.contact_person
          this.contact_tel = res.data.contact_tel
          this.is_agency = res.data.is_agency
        }).catch(error => {
            if (error.response) {
              console.log("b:", error.response.status);
            } else if (error.request) {
              console.log(error.request);
            } else {
              console.log('Error', error.message);
            }
        })
      }
    },
  },
  created() {
      this.getAgencyCompanyInfo()
      this.getQRCode()
  },

};
</script>

<style  scoped>

</style>